
var $audio = $('.audio');
var player = new Player($audio);
var progress;
var voiceProgress;
var lyric;

// 初始化歌曲信息
function initMusicInfo(music) {
    var $musicImg = $('.song_info_cover>img');
    var $musicName = $('.song_info_name>a');
    var $musicSinger = $('.song_info_singer>a');
    var $musicAlbum = $('.song_info_album>a');
    var $musicProgressName = $('.music_name>a');
    var $musicProgressSinger = $('.music_singer>a');
    var $musicProgressTime = $('.music_time');
    var $mask = $('.mask');

    // 给获取到的元素赋值
    $musicImg.attr('src', `https://y.qq.com/music/photo_new/T002R300x300M000${music.album.pmid}.jpg?max_age=2592000`);
    $musicName.text(music.name);
    $musicSinger.text(music.singer[0].name);
    $musicAlbum.text(music.album.name);
    $musicProgressName.text(music.name);
    $musicProgressSinger.text(music.singer[0].name);
    music.time = '00:00';
    $musicProgressTime.text('00:00 / ' + music.time);
    $mask.css({
        background: `url(https://y.qq.com/music/photo_new/T002R300x300M000${music.album.pmid}.jpg?max_age=2592000) no-repeat`,
        backgroundSize: 'cover',
        backgroundPosition: '50%'
    });
}

// 初始化歌词信息
function initLyric(music) {
    lyric = new Lyric(music.mid);
    // 清空上一首音乐的歌词
    $('.removeLi').remove();
    lyric.loadLyric(function () {
        // 创建歌词列表
        $.each(lyric.lyrics, function (index, value) {
            $('.lyric_content').append(`
                    <li class="removeLi">${value}</li>
                `)
            $('.lyric_content2').append(`
                    <li class="removeLi">${value}</li>
                `)
        })
    });
}

initProgress()
// 初始化进度条
function initProgress() {
    // 进度条
    var $progressBar = $('.music_progress');
    var $progressLoad = $('.music_progress_load');
    var $progressPlay = $('.music_progress_play');
    progress = Progress($progressBar, $progressLoad, $progressPlay);
    progress.progressClick(function (value) {
        player.musicSeekTo(value);
    });
    progress.progressMove(function (value) {
        player.musicSeekTo(value);
    });

    // 音量
    var $voiceBar = $('.player_progress');
    var $voiceLoad = $('.player_progress_load');
    var $voicePlay = $('.player_progress_play');
    voiceProgress = Progress($voiceBar, $voiceLoad, $voicePlay);
    voiceProgress.progressClick(function (value) {
        player.musicVoiceSeekTo(value);
    });
    voiceProgress.progressMove(function (value) {
        player.musicVoiceSeekTo(value);
    });
}

// 初始化事件监听
setTimeout(function () {
    initEvents();
}, 1000)
function initEvents() {
    // 监听子菜单播放按钮点击事件
    $('.songlist_content').delegate('.list_menu_icon_play', 'click', function () {
        var $item = $(this).parents('.songlist_list');
        // 子菜单播放按钮
        $(this).toggleClass('list_menu_icon_play2');
        $item.siblings().find('.list_menu_icon_play').removeClass('list_menu_icon_play2');

        // 子菜单序号动画
        $item.children('.songlist_number').toggleClass('songlist_item_playing');
        $item.siblings().find('.songlist_number').removeClass('songlist_item_playing');

        // 子菜单歌曲名称
        $item.children('.songlist_header_name').children('.name').toggleClass('song_list_active');
        $item.siblings().find('.name').removeClass('song_list_active');

        // 同步底部播放按钮
        if ($(this).hasClass('list_menu_icon_play2')) {
            $('.music_play').addClass('music_play2');
        } else {
            $('.music_play').removeClass('music_play2');
        }

        // 播放音乐
        player.playMusic($item.get(0).index, $item.get(0).music);

        // 切换歌曲信息
        initMusicInfo($item.get(0).music);
        // 切换歌词
        initLyric($item.get(0).music);
    })

    // 全选框
    $('.all_checkbox').click(function () {
        $('.checkbox').toggleClass('checked');
    })

    // 监听复选框点击事件
    $('.songlist_content').delegate('.checkbox', 'click', function () {
        $(this).toggleClass('checked');
    })


    // 纯净模式切换
    var flag = 1;
    $('.music_only').click(function () {
        // 隐藏歌曲页面。显示纯净页面
        if (flag == 1) {
            $('.only_content').show().prev().hide();
            $('.music_only').css('backgroundPosition', '0 -311px');
            flag = 0;
        } else {
            $('.main_content').show().next().hide();
            $('.music_only').css('backgroundPosition', '0 -282px')
            flag = 1;
        }
    })

    // 监听底部按钮 播放 点击事件
    $('.music_play').click(function () {
        // 判断是否播放过音乐
        if (player.currentIndex == -1) {
            // 没有播放过
            $('.songlist_list').eq(0).find('.list_menu_icon_play').trigger('click');
        } else {
            // 播放过音乐
            $('.songlist_list').eq(player.currentIndex).find('.list_menu_icon_play').trigger('click');
        }
    });
    // 监听底部按钮 上一首 点击事件
    $('.music_prev').click(function () {
        $('.songlist_list').eq(player.prevIndex()).find('.list_menu_icon_play').trigger('click');
    });
    // 监听底部按钮 下一首 点击事件
    $('.music_next').click(function () {
        $('.songlist_list').eq(player.nextIndex()).find('.list_menu_icon_play').trigger('click');
    });

    // 监听删除按钮点击事件
    $('.songlist_content').delegate('.del', 'click', function () {
        $item = $(this).parents('.songlist_list');
        // 判断当前删除的是否是正在播放的
        if ($item.get(0).index == player.currentIndex) {
            // 自动切换到下一首
            $('.music_next').trigger('click');
        }
        $item.remove();
        player.removeMusic($item.get(0).index);

        // 重新排序
        $('.songlist_list').each(function (index, value) {
            value.index = index;
            $(value).find('.songlist_number').text(index + 1);
        });
    })

    // 监听播放进度
    player.musicTimeUpdate(function (duration, currentTime, timeStr) {
        // 同步歌曲时间
        $('.music_time').text(timeStr);
        // 同步进度条
        // 计算播放比例
        var value = currentTime / duration * 100;
        progress.setProgress(value);

        // 歌词同步
        var index = lyric.currentIndex(currentTime);
        var $item = $('.lyric_content>li').eq(index);
        var $item2 = $('.lyric_content2>li').eq(index);
        $item.addClass('cur');
        $item.siblings().removeClass('cur');

        $item2.addClass('cur');
        $item2.siblings().removeClass('cur');

        if (index <= 2) return;

        $('.lyric_content').css({
            marginTop: ((-index + 2) * 35),
        })
        $('.lyric_content2').css({
            marginTop: ((-index + 1) * 35),
        })
    })

    // 监听声音按钮
    $('.player_voice').click(function () {
        // 图标切换
        $(this).toggleClass('player_voice2');
        // 声音切换
        if ($(this).hasClass('player_voice2')) {
            // 静音
            player.musicVoiceSeekTo(0);
        } else {
            // 关闭静音
            player.musicVoiceSeekTo(1);
        }
    })
}
